package top.codestudy.viewtest.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import top.codestudy.viewtest.R;
import top.codestudy.viewtest.common.Util;

public class NumberView extends View {
    private static final String TAG = "NumberView";
    Paint mTextPaint;
    Paint mBitmapPaint;
    private Context mContext;
    private long number;
    Bitmap girlBitmap;

    public NumberView(Context context) {
        this(context, null);
    }

    public NumberView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public NumberView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.mContext = context;
        init();
    }

    private int girlBitWidth,girlBitHeight;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(girlBitWidth,girlBitHeight);
    }

    private void init() {
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.parseColor("#FFA3A5A8"));
        mTextPaint.setTextSize(Util.sp2px(mContext, 16));
        mTextPaint.setLetterSpacing(0.1f);

        mBitmapPaint = new Paint();
        mTextPaint.setAntiAlias(true);

        girlBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.ic_launcher_round3)).getBitmap();
        girlBitWidth = girlBitmap.getWidth();
        girlBitHeight = girlBitmap.getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Camera camera = new Camera();
        Rect src = new Rect(0,0,girlBitWidth,girlBitHeight);
        //canvas.drawBitmap(girlBitmap,null,src,mBitmapPaint);
        canvas.save();
        canvas.clipRect(0,0,girlBitWidth/2,girlBitHeight);
        camera.save();
        camera.rotateX(10);
        canvas.translate(girlBitWidth/2/2,girlBitHeight/2);
        camera.applyToCanvas(canvas);
        canvas.translate(-girlBitWidth/2/2,-girlBitHeight/2);
        camera.restore();
        canvas.drawBitmap(girlBitmap,null,src,mBitmapPaint);
        canvas.restore();
        Rect src2 = new Rect(girlBitWidth/2,0,girlBitWidth,girlBitHeight);
        Rect src1 = new Rect(girlBitWidth/2,0,girlBitWidth,girlBitHeight);
        canvas.drawBitmap(girlBitmap,src2,src1,mBitmapPaint);
    }

    public void addNumber() {
        this.number++;
        invalidate();
    }

    public void setNumber(long number) {
        this.number = number;
        invalidate();
    }
}
